import {
  GET_CATETRY,
  GET_CATETRY_MODIFY1,
  GET_CATETRY_MODIFY2,
} from "../action/actionType";
const initState = {
  content_title: [],
  content_list: [],
  content_index: 0,
};
// 本地缓存
export default function (state = initState, action) {
  let newState = JSON.parse(JSON.stringify(state));
  switch (action.type) {
    case GET_CATETRY:
      // 本地缓存
      let listData = {
        // 时间戳
        time: Date.now(),
        // 获取到的数据
        list: action.payload,
      };
      newState.content_title = action.payload;
      newState.content_list = action.payload[0].children;
      localStorage.setItem("ListData", JSON.stringify(listData));
      return newState;
    case GET_CATETRY_MODIFY1:
      newState.content_title = action.payload;
      newState.content_list = action.payload[0].children;
      return newState;
    case GET_CATETRY_MODIFY2:
      let id = parseInt(action.payload);
      newState.content_index = id;
      newState.content_list = state.content_title[id].children;
      return newState;
    default:
      return state;
  }
}
